﻿/*
*  全局变量
*/
var eagleeyeControl = void (0);
var levelcontrol = void (0);
var dswitcher = void (0);
var scaleControl = void (0);
var dMap = void(0); //主界面的地图

var userPoints = []; //用户的位置。
var userIcons = []; //用户
var userPops = []; //用户信息弹出文本

var MonitorMode = false; //监视类型。true：显示所有在线用户。false：只显示超过区域的不合法用户
var xmlHttp; //XML对象

//入口函数
function MonitorMapLoad()
{
    //从XML读取 用户设置的刷新频率  UpdateInterval
    initialMaps(); //首先实现基础GIS功能

}

/**
* 整个地图全部初始化,最终功能是要求呈现出基础地图功能。不包括标记,该函数只能调用一次,并且是在地图第一次load的时候调用
*/
function initialMaps()
{
    if (dMap == null)
        dMap = new DMap(document.getElementById("mapcontainer"));
    var icenter = new DLonLat(118.5, 36.5);
    var izoom = 12;
    dMap.setCenter(icenter, izoom);
    dMap.enableScrollWheelZoom();
    dMap.addLayer(D_OVERLAY_TDT_VECTOR_LAYER);
    dMap.addLayer(D_OVERLAY_VECTOR_LAYER);
    dMap.addLayer(D_OVERLAY_DY_RASTER_LAYER);
    D_OVERLAY_DY_RASTER_LAYER.display(false);
    dMap.addLayer(D_OVERLAY_TDT_RASTER_LAYER);
    D_OVERLAY_TDT_RASTER_LAYER.display(false);
    dMap.addLayer(D_OVERLAY_DY_VECTOR_LAYER);
    //显示地图中心点坐标
    showMapCenterPosition();
    showScale();
    showLevel();
    showEagleEye();
    setKeyEnable();
    
    dMap.addListener("mousemove", showMousePosition, this);
    dMap.addListener("dragend", showMapCenterPosition, this);
    GenerateUserInfos();
}



//键盘控制地图
function setKeyEnable()
{
    dMap.enableKeyMap();
}

//显示鹰眼控件
function showEagleEye()
{
    if (!eagleeyeControl)
    {
        eagleeyeControl = new DEagleeye();
        dMap.addControl(eagleeyeControl);
        eagleeyeControl.open();
    }
    if (eagleeyeControl)
    {
        eagleeyeControl.show();
    }
}

//缩放等级工具
function showLevel()
{
    if (!levelcontrol)
    {
        levelcontrol = new DLevel({ select: 4, OffsetX: 20, pos: DControlAnchor.TOP_LEFT, top: 0 });
        dMap.addControl(levelcontrol);
    }
    if (levelcontrol)
    {
        levelcontrol.show();
    }
}


//显示比例尺控件
function showScale()
{
    if (!scaleControl)
    {
        scaleControl = new DScale();
        dMap.addControl(scaleControl);
    }
    if (scaleControl)
    {
        scaleControl.show();
    }
}
//切换底图状态

function addYX()
{
    dMap.setMapType(TDT_SATE_MAP);
    D_OVERLAY_VECTOR_LAYER.display(false);
    D_OVERLAY_TDT_RASTER_LAYER.display(true);
    D_OVERLAY_TDT_VECTOR_LAYER.display(false);
    D_OVERLAY_DY_VECTOR_LAYER.display(false);
    D_OVERLAY_DY_RASTER_LAYER.display(true);

}
//移除底图切换控件
function addSL()
{
    dMap.setMapType(TDT_VECTOR_MAP);
    D_OVERLAY_VECTOR_LAYER.display(true);
    D_OVERLAY_TDT_RASTER_LAYER.display(false);
    D_OVERLAY_TDT_VECTOR_LAYER.display(true);
    D_OVERLAY_DY_VECTOR_LAYER.display(true);
    D_OVERLAY_DY_RASTER_LAYER.display(false);
}

/*
*显示地鼠标坐标函数
*/
function showMousePosition(p)
{
    if (p == null)
    {
        return;
    }

    var Bstr = "";
    var Lstr = "";
    if (p.lat < 0)
    {
        var latTemp = 0 - p.lat;
        Bstr = "<span class=\"tipsN\">S</span>" + Rad2Deg(latTemp);
    }
    else
        Bstr = "<span class=\"tipsN\">N</span>" + Rad2Deg(p.lat);
    if (p.lon < 0)
    {
        var lonTemp = 0 - p.lon;
        Lstr = "<span class=\"tipsN\">W</span>" + Rad2Deg(lonTemp);
    }
    else
        Lstr = "<span class=\"tipsN\">E</span>" + Rad2Deg(p.lon);
    document.getElementById("mousePosition").innerHTML = "坐标:<span class=\"tipsShuzi\">" + Bstr + "</span> <span class=\"tipsShuzi\">" + Lstr + "</span>";
}


/*
*显示地图中心坐标函数
*/
function showMapCenterPosition()
{
    var center = dMap.getCenter();
    var Bstr = "";
    var Lstr = "";
    if (center.lat < 0)
    {
        var latTemp = 0 - center.lat;
        Bstr = "<span class=\"tipsN\">S</span>" + Rad2Deg(latTemp);
    }
    else
        Bstr = "<span class=\"tipsN\">N</span>" + Rad2Deg(center.lat);
    if (center.lon < 0)
    {
        var lonTemp = 0 - center.lon;
        Lstr = "<span class=\"tipsN\">W</span>" + Rad2Deg(lonTemp);
    }
    else
        Lstr = "<span class=\"tipsN\">E</span>" + Rad2Deg(center.lon);
    document.getElementById("centerPosition").innerHTML = "中心:<span class=\"tipsShuzi\">" + Bstr + "</span> <span class=\"tipsShuzi\">" + Lstr + "</span>";
}


/*
* 弧度转换为角度
*/
function Rad2Deg(deg)
{
    var d = parseInt(deg);
    var f = parseInt((deg - d) * 60);
    var m = parseInt(((deg - d) * 60 - f) * 60);
    if (f.toString().length == 1) f = "0" + f;
    if (m.toString().length == 1) m = "0" + m;
    return d + "° " + f + "\' " + m + "\" ";
}
//显示面板按钮事件
function btnDialog()
{
    var chBox = document.getElementById("Checkbox2");
    if (chBox.checked)
        $("#userListDIV").dialog('open');
    else
    {
        $("#userListDIV").dialog('close');
    }
}

//关闭面板
function closeDialog()
{
    document.getElementById("Checkbox2").checked = false;
}


function changeMonitorMode()
{
    if (isAreaClick || isDistanceClick)
    {
        return;
    }
    MonitorMode = !MonitorMode;
    if (MonitorMode)  //为true,正在显示所有用户
    {
        document.getElementById("Button5").innerHTML = "违规用户";
        document.getElementById("Errortips").innerHTML = "提示信息:目前显示所有在线用户";
    }
    else    //为false,正在显示所有非法用户
    {
        document.getElementById("Button5").innerHTML = "所有用户";
        document.getElementById("Errortips").innerHTML = "提示信息:目前仅显示违规的在线用户";
    }
    //重新显示用户的信息。
    resetMonitorMapOverLays();
    GenerateUserInfos();
}



function GenerateUserInfos()
{
    if (window.ActiveXObject)
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest)
    {
        xmlHttp = new XMLHttpRequest();
    }
    xmlHttp.onreadystatechange = handle;
    xmlHttp.open("GET", "GenerateMonitorUsers.aspx?r=", true);
    xmlHttp.send(null);

    dMap.addListener("click", function(point) {
        try {
//                        alert(point);
            for (i = 0; i < userIcons.length; i++) {
                var B1 = userPoints[i].lat;
                var B2 = userPoints[i].lat + 0.15;
                var L1 = userPoints[i].lon - 0.05;
                var L2 = userPoints[i].lon + 0.05;
                var B = point.lat;
                var L = point.lon;
                var bool1;
                var bool2;
                bool1 = ((eval(B1) < eval(B)) && (eval(B) < eval(B2)));
                bool2 = ((eval(L1) < eval(L)) && (eval(L) < eval(L2)));
                if (bool1 && bool2) {
                    userPops[i].show(true);
                }
            }
        }
        catch (e) { }
        })
}

function handle()
{
    var xmlDom;
    if (xmlHttp.readyState == 4)
    {
        if (xmlHttp.status == 200)
        {
            xmlDom = xmlHttp.responseXML;
            //定义users数组，获取所有User标签
            var users = xmlDom.documentElement.getElementsByTagName("User");
            if (users[0] == null)
            {
                alert("当前无用户");
                return false;
            }
            var innerUserListHtmlStrs = "";
            for (i = 0; i < users.length; i++)
            {
                var rtkuserid = parseInt(users[i].getAttribute("RTKUserID"));
                var addressname = users[i].getAttribute("AddressName");
                var unitname = users[i].getAttribute("UnitName");
                var departmentname = users[i].getAttribute("DepartmentName");
                var rtkusername = users[i].getAttribute("RTKUserName");
                var userpoint = new DLonLat(parseFloat(users[i].getAttribute("L")), parseFloat(users[i].getAttribute("B")));
                var userb = parseFloat(users[i].getAttribute("B"));
                var userl = parseFloat(users[i].getAttribute("L"));
                var userh = parseFloat(users[i].getAttribute("H"));
                var uservalid = users[i].getAttribute("IsValid");
                var title;
                var content;
                var config;
                var icon = void (0);
                var polygon = null;
                var pop = void (0);
                userPoints.push(userpoint);
                //不合法,红色标记，不论什么监测模式，不合法用户始终显示
                if (uservalid == 'false')
                {
                    title = "<div style='margin-top:4px;font-size:12px;color:#000FFF;'>" + "警告：违规用户" + "</div>";
                    content = "<div style='margin-top:4px;font-size:12px;color:black;'>" + "用户名：" + rtkusername + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "地区：" + addressname + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "单位-部门：" + unitname + "-" + departmentname + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "纬度：" + userb + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "经度：" + userl + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "大地高：" + userh + "</div>";
                    config = { color: "red", titleType: "html", flash: true, height: 150, width: 200, offsety: -40 };
                    if (!icon)
                    {
                        //定义点符号
                        icon = new DIcon(userpoint);
                        userIcons.push(icon);
                    }
                    //添加地图覆盖物
                    dMap.addOverlay(icon);
                    if (!pop)
                    {
                        pop = new DPopup(userpoint, title, content, config);
                        userPops.push(pop);
                        dMap.addOverlay(pop);
//                        pop.show(true);
                    }
                }
                else if (MonitorMode) //合法，绿色标记，只有在MonitorMode为true的时候才显示。为false则不显示
                {
                    title = "<div style='margin-top:4px;font-size:12px;color:#000FFF;'>" + "CORS用户监控信息" + "</div>";
                    content = "<div style='margin-top:4px;font-size:12px;color:black;'>" + "用户名：" + rtkusername + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "地区：" + addressname + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "单位-部门：" + unitname + "-" + departmentname + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "纬度：" + userb + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "经度：" + userl + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "大地高：" + userh + "</div>";
                    config = { color: "red", titleType: "html", flash: true, height: 120, width: 200, offsety: -40 };
                    if (!icon)
                    {
                        //定义点符号
                        icon = new DIcon(userpoint);
                        userIcons.push(icon);
                    }
                    //添加地图覆盖物
                    dMap.addOverlay(icon);
                    if (!pop)
                    {
                        pop = new DPopup(userpoint, title, content, config);
                        dMap.addOverlay(pop);
                        userPops.push(pop);
//                        pop.show(true);
                    }
                }

                if (rtkuserid != "null" && rtkuserid != "0")
                {
                    if (uservalid == 'false')
                    {
                        var DiscrptStrTemp = (i + 1).toString() + ". " + rtkusername + "-" + addressname + "-" + unitname + "-" + "违规作业" + "<br /><br />";
                    }
                    else
                    {
                        var DiscrptStrTemp = (i + 1).toString() + ". " + rtkusername + "-" + addressname + "-" + unitname + "-" + "合法作业" + "<br /><br />";
                    }
                    var clickeventProStr = 'onclick="onUserListItemClick(' + users[i].getAttribute("L") + ',' + users[i].getAttribute("B") + ')"';
                    var atempHtml = '<a href="#" ' + clickeventProStr + ' >' + DiscrptStrTemp + '</a>';
                    innerUserListHtmlStrs += atempHtml;
                }
                document.getElementById("userListDIV").innerHTML = innerUserListHtmlStrs;
            }
        }
    }
}

function onUserListItemClick(userLStr, userBstr)
{
    var CentergeoPoint = new DLonLat(parseFloat(userLStr), parseFloat(userBstr));
    dMap.setCenter(CentergeoPoint, 7);
}

function resetMonitorMapOverLays()
{
    var i;
    if (userPoints) {
        for (i = 0; i < userPoints.length; i++)
            dMap.removeOverlay(userPoints[i]);
        userPoints.length = 0;
    }
    if (userIcons) {
        for (i = 0; i < userIcons.length; i++)
            dMap.removeOverlay(userIcons[i]);
        userIcons.length = 0;
    }
    if (userPops) {
        for (i = 0; i < userPops.length; i++)
            dMap.removeOverlay(userPops[i]);
        userPops.length = 0;
    }
}